home *** CD-ROM | disk | FTP | other *** search
- /* Original by Pete Gontier
- Hacked to work as MPW tool by Sak Wathanasin
- # Build an MPW tool
- C -d MPW uuencode.c ╖╖ "{Worksheet}"
- Link -w -o uuencode -t MPST -c "MPS " uuencode.c.o ╢
- "{CLibraries}"StdCLib.o ╢
- "{Libraries}"ToolLibs.o ╢
- "{Libraries}"Runtime.o ╢
- "{Libraries}"Interface.o ╖╖ "{Worksheet}"
- delete uuencode.c.o ╖╖ "{Worksheet}"
- */
-
- #ifdef MPW
- #include <Types.h>
- #include <StdDef.h>
- #include <Files.h>
- #include <CursorCtl.h>
- #include <StdLib.h>
- #endif MPW
- #include <errno.h>
- #include <stdio.h>
- #include <String.h>
- #ifndef NIL
- #define NIL (0L)
- #endif
-
- /* Strings used in error messages */
- #ifdef MPW
- #define CPSTR c2pstr
- #define PCSTR p2cstr
- #else
- #define CPSTR CtoPstr
- #define PCSTR PtoCstr
- #endif
-
- #ifndef EOF
- #define EOF (-1L)
- #endif
-
- Boolean unix = false; /* convert CR<->LF for Unix */
- Boolean decodeIt = false; /* assume encode */
- OSType fdCreator = '????'; /* Finder Creator */
- OSType fdType = '????'; /* Finder Type */
-
- /* error codes */
-
- #define OTHER -1 /* something strange happened */
- #define NOERR 0 /* everything's hunky-dory */
- #define NOOPIN 1 /* opening input file */
- #define NOOPOUT 2 /* opening output file */
- #define NOREAD 3 /* reading input */
- #define NOWRITE 4 /* writing output */
- #define NOCLIN 5 /* closing input */
- #define NOCLOUT 6 /* closing output */
- #define FORMAT 7 /* file is in an unreadable format */
- #define UEOF 8 /* unexpected end-of-file */
- #define DETRANS 9 /* decompression or translation error */
-
- int uudecode ( char *source );
- int uuencode ( char *source );
-
- int fr ( FILE *fd, char *buf, int cnt );
- int decode ( FILE *in, FILE *out );
- int doutdec(char *p, FILE *f, int n);
- char *index ( char *sp, char c );
- int encode( FILE *in, FILE *out);
- int eoutdec ( char *p, FILE *f);
-
- int fr ( FILE *fd, char *buf, int cnt ) {
- int c, i;
-
- for (i=0; i<cnt; i++) {
- c = getc(fd);
- if (c == EOF)
- return(i);
- buf[i] = c;
- }
- return (cnt);
- }
-
- /************************************************************************/
-
- /* single character decode */
- #define DEC(c) (((c) - ' ') & 077)
-
- int uudecode ( char *source ) {
- FILE *in, *out;
- int mode, result;
- char dest[31], buf[80];
-
- if (source == NULL ||
- *source == '-' )
- in = stdin;
- else
- if ( ( in = fopen ( source, "r" ) ) == NULL )
- return ( NOOPIN );
-
- /* search for header line */
- for (;;) {
- if (fgets(buf, sizeof buf, in) == NULL)
- return ( FORMAT );
- if (strncmp(buf, "begin ", 6) == 0)
- break;
- }
- sscanf(buf, "begin %o %s", &mode, dest);
-
- /* create output file */
- if (*dest == '-' || in == stdin)
- out = stdout;
- else
- out = fopen(dest, (unix? "w" : "wb"));
- if (out == NULL)
- return ( NOOPOUT );
-
- if ( result = decode (in, out) )
- return ( result );
-
- if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n"))
- return ( DETRANS );
-
- /* set the file type before we exit */
- fsetfileinfo(dest, fdCreator, fdType);
- return ( NOERR );
- }
-
- int decode ( FILE *in, FILE *out ) {
- char buf[80];
- char *bp;
- int n, result;
-
- for (;;) {
- /* for each input line */
- SpinCursor(-1);
- if (fgets(buf, sizeof buf, in) == NULL)
- return ( NOREAD );
- n = DEC(buf[0]);
- if (n <= 0)
- break;
-
- bp = &buf[1];
- while (n > 0) {
- if ( result = doutdec(bp, out, n) )
- return ( result );
- bp += 4;
- n -= 3;
- }
- }
- return ( NOERR );
- }
-
- int doutdec(char *p, FILE *f, int n) {
- char c1, c2, c3;
-
- c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
- c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
- c3 = DEC(p[2]) << 6 | DEC(p[3]);
- if (n >= 1) {
- if (unix && c1 == '\r') c1 = '\n';
- if ( EOF == putc(c1, f) )
- return ( NOWRITE );
- }
- if (n >= 2) {
- if (unix && c2 == '\r') c2 = '\n';
- if ( EOF == putc(c2, f) )
- return ( NOWRITE );
- }
- if (n >= 3) {
- if (unix && c3 == '\r') c3 = '\n';
- if ( EOF == putc(c3, f) )
- return ( NOWRITE );
- }
- return ( NOERR );
- }
-
- char *index ( char *sp, char c ) {
- do {
- if (*sp == c)
- return(sp);
- } while (*sp++);
- return(NULL);
- }
-
- /**********************************************************************/
-
- /* ENC is the basic 1 character encoding function to make a char printing */
- #define ENC(c) (((c) & 077) + ' ')
-
- int uuencode (char *source) {
- FILE *in, *out;
- int result;
-
- if (source == NULL ||
- *source == '-' ) {
- in = stdin;
- source = "-";
- }
- else
- if ( ( in = fopen ( source, "r" ) ) == NULL )
- return ( NOOPIN );
-
- out = stdout;
-
- if (*source == 'ñ') source = "-";
- fprintf(out, "begin %o %s\n", 022, source);
- if ( ferror ( out ) )
- return ( NOWRITE );
-
- if ( NOERR != ( result = encode(in, out) ) )
- return ( result );
-
- fprintf(out, "end\n");
- if ( ferror ( out ) )
- return ( NOWRITE );
-
- return ( NOERR );
- }
-
- int encode( FILE *in, FILE *out) {
- char buf[80];
- int i, n, result;
-
- for (;;) {
- /* 1 (up to) 45 character line */
- SpinCursor(-1);
- n = fr(in, buf, 45);
- if (unix) {
- /* convert CR->LFs */
- for (i = 0; i < n; i++)
- if ( *(buf + i) == '\n' )
- *(buf + i) = '\r';
- }
- if ( EOF == putc(ENC(n), out) )
- return ( NOWRITE );
-
- for (i=0; i<n; i += 3)
- if ( NOERR != ( result = eoutdec(&buf[i], out) ) )
- return ( result );
-
- if ( EOF == putc('\n', out) )
- return ( NOWRITE );
- if (n <= 0)
- break;
- }
- return ( NOERR );
- }
-
- int eoutdec ( char *p, FILE *f) {
- int c1, c2, c3, c4;
-
- c1 = *p >> 2;
- c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
- c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
- c4 = p[2] & 077;
- if ( EOF == putc(ENC(c1), f) )
- return ( NOWRITE );
- if ( EOF == putc(ENC(c2), f) )
- return ( NOWRITE );
- if ( EOF == putc(ENC(c3), f) )
- return ( NOWRITE );
- if ( EOF == putc(ENC(c4), f) )
- return ( NOWRITE );
- return ( NOERR );
- }
-
- main(int argc, char** argv)
- {
- char* theFile = NULL;
- #ifdef MPW
- InitCursorCtl(nil);
- #endif MPW
- argc--; argv++;
-
- while (argc) {
- if (argv[0][0] == '-') {
- switch (argv[0][1]) {
- case '\0':
- theFile = "-";
- break;
- case 'c':
- /* creator is in next arg */
- argc--;
- argv++;
- strncpy((char *)&fdCreator, argv[0], (size_t)4);
- break;
- case 't':
- /* type is in next arg */
- argc--;
- argv++;
- strncpy((char *)&fdType, argv[0], (size_t)4);
- break;
- case 'u':
- unix = true;
- break;
- case 'd':
- decodeIt = true;
- break;
- default:
- fprintf(stderr, "### Usage: uuencode [-d [-u] [-c creator] [-t type]] [file|-]\n");
- exit(-1);
- }
- }
- else
- theFile = argv[0];
- argc--; argv++;
- }
- if (decodeIt)
- uudecode(theFile);
- else
- uuencode(theFile);
- }
-